﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OTDR_OPT
{
    /// <summary>
    /// ProductEdit.xaml 的交互逻辑
    /// </summary>
    public partial class ProductEdit : Window
    {

        public ProductEdit()
        {
            InitializeComponent();
            InitProductList(); //初始化界面中的机型列表
        }


        //读取机型清单，显示在列表中
        private void InitProductList()
        {
            List<string> productList = ProductManagement.GetInstance().GetProductList();
            ProductNameDataGrid.ItemsSource = null;
            ProductNameDataGrid.ItemsSource = productList;
        }


        //添加机型
        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            string productname = ProductNameGrid.Text.Trim();
            if (productname == "")
            {
                MessageBox.Show("产品型号为空","无法添加空型号",MessageBoxButton.OK,MessageBoxImage.Information);
                return;
            }
            #region 获取选中的波长列表，放入一个List
            CheckBox[] checkBoxArr = { CheckBox850, CheckBox1300, CheckBox1310, CheckBox1490, CheckBox1550, CheckBox1625, CheckBox1650 };
            List<string> wavelengthList = new List<string>();
            for(int i = 0; i < checkBoxArr.Length; i++)
            {
                if ((bool)checkBoxArr[i].IsChecked)
                {
                    wavelengthList.Add(checkBoxArr[i].Content.ToString());
                }
            }
            #endregion
            #region 获取线性点，放入一个List
            TextBox[] linearBoxArr = { P1_0, P2_0, P1_1, P2_1, P1_2, P2_2, P1_3, P2_3, P1_4, P2_4, P1_5, P2_5, P1_6, P2_6};
            List<Linear> linearList = new List<Linear>();
            for(int i = 0 ; i<linearBoxArr.Length ; i++)
            {
                int index = i / 2;
                string p1String = linearBoxArr[i].Text.Trim();
                i++;
                string p2String = linearBoxArr[i].Text.Trim();
                if (p1String != "" && p2String != "")
                {
                    double p1 = Convert.ToDouble(p1String);
                    double p2 = Convert.ToDouble(p2String);
                    Linear linear = new Linear();
                    linear.Index = index;
                    linear.P1 = p1;
                    linear.P2 = p2;
                    linearList.Add(linear);
                }
            }
            #endregion
            Product p = new Product();
            p.ProductName = productname;
            p.Wavelengthes = wavelengthList;
            p.Linears = linearList;
            ProductManagement.GetInstance().SaveData(p);
            MessageBox.Show("保存机型" + productname + "完成" , "保存完成" ,MessageBoxButton.OK,MessageBoxImage.Information);
            //刷新列表
            InitProductList();
        }


        //删除机型
        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            string productname = ProductNameGrid.Text.Trim();
            if (productname == "")
            {
                //MessageBox.Show("产品型号为空", "无法添加空型号", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            MessageBoxResult clickButton = MessageBox.Show("是否确定要删除" + productname,"删除机型",MessageBoxButton.YesNo,MessageBoxImage.Question);
            if(clickButton == MessageBoxResult.Yes)
            {
                ProductManagement.GetInstance().DeleteProduct(productname);
            }
            //刷新列表
            InitProductList();
        }


        //选中机型时，将它的机型名称、波长列表、线性点显示在右侧
        private void ProductNameDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            int index = ProductNameDataGrid.SelectedIndex;
            if (index < 0)
            {
                return;
            }
            List<string> productnameList = ProductManagement.GetInstance().GetProductList();
            string productname = productnameList[index];
            List<string> wavlengthList = ProductManagement.GetInstance().GetWaveList(productname);
            //显示机型
            ProductNameGrid.Text = productname;
            //显示波长列表
            CheckBox[] checkBoxArr = { CheckBox850, CheckBox1300, CheckBox1310, CheckBox1490, CheckBox1550, CheckBox1625, CheckBox1650 };
            foreach(CheckBox check in checkBoxArr)
            {
                if (wavlengthList.Contains(check.Content.ToString()))
                {
                    check.IsChecked = true;
                }
                else
                {
                    check.IsChecked = false;
                }
            }
            //显示机型对应的线性点
            List<Linear> linearList = ProductManagement.GetInstance().GetLinearList(productname);
            List<TextBox> linearBoxList = (new TextBox[]{ P1_0 , P2_0, P1_1 , P2_1, P1_2, P2_2 , P1_3 , P2_3 , P1_4 , P2_4 , P1_5 , P2_5  ,P1_6 , P2_6}).ToList();
            List<TextBox> newValueBoxList = new List<TextBox>();
            if (linearList != null)
            {
                foreach (Linear linear in linearList)
                {
                    int linearIndex = linear.Index;
                    double p1 = linear.P1;
                    double p2 = linear.P2;
                    TextBox p1Box = linearBoxList[linearIndex * 2];
                    TextBox p2Box = linearBoxList[linearIndex * 2 + 1];
                    p1Box.Text = p1.ToString();
                    p2Box.Text = p2.ToString();
                    newValueBoxList.Add(p1Box);
                    newValueBoxList.Add(p2Box);
                }
            }
            //不在配置文件中的线性点数据清空
            foreach(TextBox linearTextBox in linearBoxList)
            {
                if (!newValueBoxList.Contains(linearTextBox))
                {
                    linearTextBox.Text = "";
                }
            }
        }


        //线性点内容发生变化时，校验是否可以转为浮点数,如果不能转为浮点数，显示红色
        private void LinearText_Changed(object sender,TextChangedEventArgs args)
        {
            TextBox textBox = sender as TextBox;
            string text = textBox.Text.Trim();
            try
            {
                double d = Convert.ToDouble(text);
                textBox.Foreground = Brushes.Black;
            }
            catch (Exception)
            {
                textBox.Foreground = Brushes.Red;
            }
        }
    }
}
